The first two cells below are used to tell the notebook that we want any plots (generated by matplotlib) to appear inline with the notebook and not in a separate window.
In [68]:
%matplotlib inline
In [69]:
import matplotlib.pyplot as plt
Next, we import all of the functions that come with the numpy package. This gives python a lot of array-based features that are optimized for computational speed.
In [70]:
from numpy import *
The first is the command linspace
linspace creates an array filled with values set by the function parameters:
In [71]:
x = linspace(0,100, num=100)
In [72]:
x
Out[72]:
You can also find out more about a function by asking for help on the command line. Type the function name and a question mark:
In [73]:
linspace?
You should see a line that says:
Signature: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
This describes the function and what values you can specify. We used only the start and stop but we could also use the num parameter to tell it how many points we want in the array.
You can close the help by typing esc or by clicking on the small x in the upper right corner.
In [86]:
x = linspace(0,100,num=1024)
x
Out[86]:
Starting from the example below, generate a few plots for practice:
Hints: for these, you'll want to have a larger number of points in the array and we don't necessarily need to view the array each time, that was just for learning purposes. Try setting num=1024 in the linspace function.
The following functions will be useful (these are both part of numpy):
random.random()fft.fft()The dot indicates that the function is part of a module within numpy; the fft function is part of the fft module so you have to specify that when you use it. You can learn more about them with the help feature:
random.random?
In [87]:
y = sin(x)
In [88]:
plt.plot(x,y)
Out[88]:
In [89]:
plt.plot(x,y,".") #another example, with dots this time...
Out[89]:
In [90]:
random.random()
Out[90]:
In [117]:
fft.fft(y)
newy=fft.fft(y)
In [119]:
plt.plot(abs(newy[0:100]))
Out[119]:
In [100]:
newarry = y + .1*random.random(1024)
In [101]:
newarry
Out[101]:
In [ ]:
In [102]:
plt.plot(x,newarry)
Out[102]:
In [103]:
fft.fft(newarry)
Out[103]:
In [121]:
plt.plot(abs(fft.fft(newarry))[0:40])
Out[121]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: